package com.suptracking.web.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;





import com.suptracking.crypt.BCrypt;
import com.suptracking.entity.User;
import com.suptracking.service.UserService;

/**
 * Servlet implementation class editUserServlet
 */
@WebServlet("/RegisteredContent/ManageUser")
public class ManageUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
@EJB
UserService userservice;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect(request.getServletContext().getContextPath()+"/ManageUser.jsp");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		HttpSession UserSession = request.getSession();
		User _user = (User) UserSession.getAttribute("User");
		
		
		if(request.getParameter("n_firstName")!=null){
			

			String new_firstname= request.getParameter("n_firstName");
			
			_user.setFirstName(new_firstname);
			
			
		}
		
	
		if(request.getParameter("n_lastName")!=null){
			
			String new_lastname = request.getParameter("n_lastName");
			
			_user.setLastName(new_lastname);
		}
		
		if(request.getParameter("newmdp")!=null&&request.getParameter("oldmdp")!=null){
			
			String nouveaumdp =BCrypt.hashpw( request.getParameter("newpwd"), BCrypt.gensalt());
			String ancienmdp = BCrypt.hashpw( request.getParameter("oldpwd"), BCrypt.gensalt());
			
			if (ancienmdp.equals(_user.getPassword())){
				
				_user.setPassword(nouveaumdp);
				
			}
			
			
		}
if(request.getParameter("n_mail")!=null){
			

			String new_mail= request.getParameter("n_mail");
			
			_user.setMailAdress(new_mail);
			
			
		}
if(request.getParameter("n_adresse")!=null){
	

	String new_adress= request.getParameter("n_adresse");
	
	_user.setMailAdress(new_adress);
	
	
}
if(request.getParameter("n_tel")!=null){
	

	String new_tel= request.getParameter("n_tel");
	
	_user.setPhoneNumber(new_tel);
	
	
}
		
		
		
		userservice.editUser(_user);
		request.setAttribute("User", _user);
	 request.getRequestDispatcher("/ManageUser.jsp").forward(request, response);;
        
		
		
	}

}
